
package com.heimali.happyreader.core.bean;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.heimali.happyreader.HappyReaderApplication;
import com.heimali.happyreader.config.Constants;
import com.heimali.happyreader.utils.AESUtils;

public class User {

    private String mUid;
    private String mUsername;
    private String mToken;
    private String mSeed;
    
    public User() {
        
    }
    
    public User(String uid) {
        mUid = uid;
    }
    
    public User(String uid, String username, String token) {
        mUid = uid;
        mUsername = username;
        mToken = token;
    }
    
    public String getUid() {
        return String.valueOf(100000+Integer.valueOf(mUid));
    }
    
    public void setUid(String uid) {
        mUid = uid;
    }
    
    public String getUsername() {
        return mUsername;
    }
    
    public void setUsername(String username) {
        mUsername = username;
    }
    
    public String getToken() {
        return mToken;
    }
    
    public void setToken(String token) {
        mToken = token;
    }
    
    public User load() {
        SharedPreferences pref = HappyReaderApplication.getContext().getSharedPreferences(getSPKey(), Context.MODE_PRIVATE);
        if (pref == null) {
            return null;
        }
        
        initSeed();
        
        String uid = pref.getString(Constants.USER_UID, null);
        String username = pref.getString(Constants.USER_USERNAME, null);
        String token = pref.getString(Constants.USER_TOKEN, null);
        
        try {
            uid = AESUtils.decrypt(mSeed, uid);
            username = AESUtils.decrypt(mSeed, username);
            token = AESUtils.decrypt(mSeed, token);
        } catch(Exception e) {
        }
        
        if (uid == null || username == null || token == null) {
            return null;
        }
        
        return new User(uid, username, token);
    }

    public void save() {
        if (TextUtils.isEmpty(mUid) || TextUtils.isEmpty(mUsername) || TextUtils.isEmpty(mToken)) {
            return ;
        }
        
        initSeed();
        
        try {
            SharedPreferences pref = HappyReaderApplication.getContext().getSharedPreferences(getSPKey(), Context.MODE_PRIVATE);
            Editor editor = pref.edit();
            editor.putString(Constants.USER_UID, AESUtils.encrypt(mSeed, mUid));
            editor.putString(Constants.USER_USERNAME, AESUtils.encrypt(mSeed, mUsername));
            editor.putString(Constants.USER_TOKEN, AESUtils.encrypt(mSeed, mToken));
            editor.commit();
        } catch (Exception e) {

        }
    }
    
    private void initSeed() {
        if (TextUtils.isEmpty(mSeed)) {
            User cUser = HappyReaderApplication.commonUserInfo;
            mSeed = Constants.USER_SEED + cUser.getUsername();
        }
    }
    
    private static String getSPKey() {
        return "user_config";
    }
}
